java recursiveaction java并发线程

葫芦的运维日志

下一篇 搜索 上一篇

浏览量 3865

2015/01/12 01:45


package com.lzw;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.TimeUnit;

/**
 * @作者 王梓
 * 莫失莫忘 仙寿恒昌
 * 2015年1月11日
 * Email:277215243@qq.com
 */
public class Test extends RecursiveAction {
      
public static void main(String args[]) throws InterruptedException{
      ForkJoinPool pool=new ForkJoinPool();
       pool.submit(new Test(0,30));
       pool.awaitTermination(2, TimeUnit.SECONDS);
       pool.shutdown();
}

/* (non-Javadoc)
 * @see java.util.concurrent.RecursiveAction#compute()
 */
private static final int threshold=5;
private int start ;
private int end ;
public Test(int start ,int end){
       this.start =start ;
       this.end =end ;
}


@Override
protected void compute() {
       // TODO Auto-generated method stub

       if(end -start <threshold){
             for(int i =start ;i <end;i++){
                  System. out.println(Thread.currentThread().getName()+ " i value:"+i);
            }
      } else{
             int middle =(start +end )/2;
            Test t1= new Test(start ,middle );
            Test t2= new Test(middle ,end );
            
             t1.fork();
             t2.fork();
      }
}
}

 

葫芦的运维日志

打赏

上一篇 搜索 下一篇
© 冰糖葫芦甜(bthlt.com) 2021 王梓打赏联系方式 陕ICP备17005322号-1